package com.youku.component.report;

import java.util.LinkedList;
import java.util.List;

public abstract class AbstractTestReport implements TestReport {

	protected List<TestRecord> records = new LinkedList<>();

	private int successCount;

	private int failCount;

	private boolean passed = true;

	protected String projectId;

	@Override
	public void addTestRecord(TestRecord record) {
		switch (record.getStatus()) {
		case SUCCESS:
			successCount++;
			break;
		case FAIL:
			failCount++;
			passed = false;
			break;
		default:
			throw new IllegalArgumentException("测试记录中的执行状态" + record.getStatus() + "不合法");
		}
		records.add(record);
	}

	@Override
	public int totalSize() {
		return records.size();
	}

	@Override
	public int success() {
		return successCount;
	}

	@Override
	public int fail() {
		return failCount;
	}

	@Override
	public boolean passed() {
		return passed;
	}

	@Override
	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	@Override
	public String simpleDisplay() {
		StringBuilder builder = new StringBuilder();
		builder.append("测试项目").append(projectId).append("一共有").append(totalSize()).append("个测试用例。测试结果为：")
				.append(result()).append("。其中，通过").append(success()).append("个，失败").append(fail()).append("个。");
		
		return builder.toString();
	}

	protected String result() {
		return passed ? "通过" : "未通过";
	}

}
